package yygh.order.Service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import yygh.hospital.enums.OrderStatusEnum;
import yygh.hospital.enums.PaymentStatusEnum;
import yygh.hospital.enums.PaymentTypeEnum;
import yygh.hospital.model.order.OrderInfo;
import yygh.hospital.model.order.PaymentInfo;
import yygh.hospital.vo.order.SignInfoVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yygh.common.helper.HttpRequestHelper;
import yygh.hosp.client.HospFeignClient;
import yygh.order.Mapper.OrderMapper;
import yygh.order.Mapper.PaymentMapper;
import yygh.order.Service.OrderService;
import yygh.order.Service.PaymentService;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class PaymentServiceImp extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {


    @Autowired
    PaymentMapper paymentMapper;

    @Autowired
    HospFeignClient hospFeignClient;

    @Autowired
    OrderService orderService;
    @Autowired
    OrderMapper orderMapper;

    //Service层，实现支付记录的添加
    @Override
    public void savePaymentInfo(OrderInfo order, Integer paymentType) {

        //首先判断该条支付记录是否存在
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("order_id",order.getId());
        queryWrapper.eq("payment_status",paymentType);
        Integer count = paymentMapper.selectCount(queryWrapper);
        if(count > 0) {
            return;
        }
        //若存在，则向支付记录表中添加对应的信息
        PaymentInfo paymentInfo=new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(order.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(order.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(order.getReserveDate()).toString("yyyy-MM-dd")+"|"+order.getHosname()+"|"+order.getDepname()+"|"+order.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(order.getAmount());
        baseMapper.insert(paymentInfo);
    }

    //Service层，实现订单支付状态的更新
    @Override
    public void paySuccess(String out_trade_no, Map<String, String> resultMap) {
        //利用mybatis-plus根据订单编号得到支付记录
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("out_trade_no",out_trade_no);
        queryWrapper.eq("payment_type", PaymentTypeEnum.WEIXIN);
        PaymentInfo paymentInfo = paymentMapper.selectOne(queryWrapper);
        //更新订单支付记录
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());//支付状态
        paymentInfo.setCallbackTime(new Date());//回调时间
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));//交易编号
        paymentInfo.setCallbackContent(resultMap.toString());//回调信息
        baseMapper.updateById(paymentInfo);
        //根据订单号获取订单记录
        //远程调用HospitalFeignClient
        OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
        //更新订单信息
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderMapper.updateById(orderInfo);

        //调用医院接口，更新订单支付信息
        SignInfoVo signInfoVo = hospFeignClient.getSignInfoVo(orderInfo.getHoscode());
        Map<String,Object> reqMap = new HashMap<>();
        reqMap.put("hoscode",orderInfo.getHoscode());
        reqMap.put("hosRecordId",orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sign", sign);
        //利用HttpRequestHelper进行医院接口的调用
        HttpRequestHelper.sendRequest(reqMap,signInfoVo.getApiUrl()+"/order/updatePayStatus");
    }

    //Service实现方法：根据orderId和paymentType获取支付PaymentInfo对象
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, PaymentTypeEnum paymentType) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = paymentMapper.selectOne(queryWrapper);
        return  paymentInfo;
    }
}
